import re
import argparse

def parse_moves(moves):
    move_pattern = r"Player ([-\d]+), enter your move \(e\.g\., a1\): ([a-s]\d{1,2})"
    player_moves = []
    for move in re.findall(move_pattern, moves):
        player = move[0]
        coord = move[1]
        player_moves.append((player, coord))
    return player_moves

def coord_to_sgf(coord):
    column_map = {chr(i): chr(i - ord('a') + ord('A')) for i in range(ord('a'), ord('s') + 1)}
    row, col = coord[1:], coord[0]
    return f"{column_map[col]}{row}"

def generate_sgf(moves):
    sgf_content = "(;GM[1]FF[4]SZ[19]"
    for player, move in moves:
        color = "B" if player == "1" else "W"
        sgf_content += f";{color}[{coord_to_sgf(move)}]"
    sgf_content += ")"
    return sgf_content

def main():
    parser = argparse.ArgumentParser(description="Convert game output to SGF format.")
    parser.add_argument("input_file", help="Path to the input text file containing game output.")
    parser.add_argument("output_file", help="Path to the output SGF file.")
    args = parser.parse_args()
    
    with open(args.input_file, "r") as file:
        game_output = file.read()
    
    moves = parse_moves(game_output)
    sgf_content = generate_sgf(moves)
    
    with open(args.output_file, "w") as sgf_file:
        sgf_file.write(sgf_content)

if __name__ == "__main__":
    main()